package com.example.xunua_mymusic.fragment;


import android.app.AlertDialog;
import android.os.Bundle;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.xunua_mymusic.R;
import com.example.xunua_mymusic.utils.FragmentUtils;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class localMusicFragment extends Fragment implements View.OnClickListener {
    RadioButton rb1,rb2,rb3,rb4;
    ViewPager viewpager;
    ArrayList<Fragment> arrayList;
    ImageView img_check;
    AlertDialog.Builder builder;
    MyAdapter myAdapter;
    int[] rbs={R.id.fragment_local_music_rb1,R.id.fragment_local_music_rb2,R.id.fragment_local_music_rb3,R.id.fragment_local_music_rb4};


    public localMusicFragment() {
        // Required empty public constructor
//        myAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view=getLayoutInflater().inflate(R.layout.fragment_local_music,null,false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View view=inflater.inflate(R.layout.fragment_local_music, container, false);
        ImageView img_back = view.findViewById(R.id.img_back);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fragment_back(view);
            }
        });
        rb1=view.findViewById(R.id.fragment_local_music_rb1);
        rb2=view.findViewById(R.id.fragment_local_music_rb2);
        rb3=view.findViewById(R.id.fragment_local_music_rb3);
        rb4=view.findViewById(R.id.fragment_local_music_rb4);
        rb1.setOnClickListener(this);
        rb2.setOnClickListener(this);
        rb3.setOnClickListener(this);
        rb4.setOnClickListener(this);
        viewpager =view.findViewById(R.id.fragment_local_music_viewpager);
        arrayList=new ArrayList<>();
        arrayList.add(new local_music_Fragment_first());
        arrayList.add(new local_music_Fragment_second());
        arrayList.add(new local_music_Fragment_third());
        arrayList.add(new local_music_Fragment_four());
        FragmentManager fragmentManager=getChildFragmentManager();
        myAdapter = new MyAdapter(fragmentManager);
        viewpager.setAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();
        rb1.performClick();
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                RadioButton rb=view.findViewById(rbs[position]);
                rb.performClick();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        img_check=view.findViewById(R.id.fragment_local_music_img_check);
        img_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View alertView = getLayoutInflater().inflate(R.layout.alertdialog_item_local_music, null, false);
                builder=new AlertDialog.Builder(getContext());
                builder.setView(alertView);
                ListView lv=alertView.findViewById(R.id.alertdialog_item_local_music);
                String[] titles={"扫描音乐","扫描"};
                ArrayAdapter simpleAdapter=new ArrayAdapter(getContext(),android.R.layout.simple_list_item_1,titles);
                lv.setAdapter(simpleAdapter);
//                builder.setCancelable(false);
                final AlertDialog show = builder.show();
                show.setCanceledOnTouchOutside(false);
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        switch (position){
                            case 0:
                                Toast.makeText(getContext(), "哦", Toast.LENGTH_SHORT).show();
                                break;
                        }
                        show.dismiss();
                    }
                });
            }
        });
        return view;
    }
    void fragment_back(View view){
        FragmentUtils fragmentUtils=new FragmentUtils();
        fragmentUtils.show(getFragmentManager(),0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fragment_local_music_rb1:
                viewpager.setCurrentItem(0);
                break;
            case R.id.fragment_local_music_rb2:
                viewpager.setCurrentItem(1);
                break;
            case R.id.fragment_local_music_rb3:
                viewpager.setCurrentItem(2);
                break;
            case R.id.fragment_local_music_rb4:
                viewpager.setCurrentItem(3);
                break;
        }
    }

    private class MyAdapter extends FragmentPagerAdapter{

        public MyAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return arrayList.get(position);
        }

        @Override
        public int getCount() {
            return arrayList.size();
        }
    }
}
